$(document).ready(function() {
	TableObj.oTableInit();
});

// 查询列表
var searchUrl = "/yyJob/console/job/listJob";
// 查询任务列表
var searchStepUrl = "/yyJob/console/job/listStep";
// 批量关闭
var batchCloseJobUrl = "/yyJob/console/job/batchCloseJob";
// 批量重启
var batchResetJobUrl = "/yyJob/console/job/batchResetJob";
// 跳过任务
var skipJobStepUrl = "/yyJob/console/job/skipStep";

// 预留，可以接入登录相关功能
var checkLogin = function(data) {

}

var TableObj = {
	oTableInit: function() {
		$('#table').bootstrapTable({
			striped: true,
			pagination: true,
			detailView: true,
			smartDisplay:false,
			showRefresh:true,
			pageNumber: 1,
			pageSize: 20,
			pageList: [20,50,100],
			queryParamsType: '',
			method: 'post',
			queryParams: queryParams,
			url: searchUrl,
			responseHandler: function(res) {
				var result = res["result"];
				return {
					"total": result.totalItem,
					"rows": result.resultList
				};
			},
			sidePagination: "server",
			showToggle: false,
			showRefresh: false,
			search: false,
			strictSearch: true,
			clickToSelect: true,
			toolbar: '#toolbar',//工具栏
			toolbarAlign: 'right',//工具栏的位置
			columns: [
				{
					checkbox: true
				},
				{
					field: 'id',
					title: '任务ID',
					formatter: function(value, row, index) {
						var e = '<a href="logListPage?jobId=' + row.id + '"  target="_blank">'+row.id+'</a> ';
						return e;
					}
				}, {
					field: 'projectCode',
					title: '项目code'
				}, {
					field: 'moduleCode',
					title: '模块code'
				}, {
					field: 'jobCode',
					title: '任务code'
				}, {
					field: 'jobName',
					title: '任务名称'
				}, {
					field: 'businessNo',
					title: '业务号'
				}, {
					field: 'jobStatusName',
					title: '任务状态',
					align: 'center',
					formatter: function(value, row, index) {
						if(row.jobStatus == "S"){
							return '<h4><span class="label label-success">' + value + '</span></h4>';
						}
						else if(row.jobStatus == "G"){
							return '<h4><span class="label label-success">' + value + '</span></h4>';
						}
						else if(row.jobStatus == "F"){
							return '<h4><span class="label label-danger">' + value + '</span></h4>';
						}
						else if(row.jobStatus == "C"){
							return '<h4><span class="label label-info">' + value + '</span></h4>';
						}
						else if(row.jobStatus == "L"){
							return '<h4><span class="label label-warning">' + value + '</span></h4>';
						}
					}
				}, {
					field: 'gmtCreatedFormat',
					title: '创建时间'
				}, {
					field: 'expectedStartTimeFormat',
					title: '期望开始执行时间'
				}, {
					field: 'execTimeFormat',
					title: '最近执行时间'
				}, {
					field: 'version',
					title: '版本'
				}, {
					field: 'execCount',
					title: '执行次数'
				}, {
					field: 'modifier',
					title: '操作人'
				}, {
					title: '操作',
					field: 'jobStatus',
					align: 'center',
					formatter: function(value, row, index) {
						var d = "";
						if (value == 'F') {
							//d += '<a onclick="resetJob(' + row.id + ')">重启</a> ';
							d += '<button type="button" class="btn btn-success" onclick="resetJob(' + row.id + ')">重启</button>';
						}
						if (value == 'F') {
							d += '<button type="button" class="btn btn-warning" style="margin-left: 2px" onclick="closeJob(' + row.id + ')">关闭</button>';
							//d += '<a onclick="closeJob(' + row.id + ')">关闭</a> ';
						}
						return d;
					}
				}],
			onExpandRow: function(index, row, $detail) {
				TableObj.initSubTable(index, row, $detail);
			}
		});
	},
	initSubTable: function(index, row, $detail) {
		var jobid = row.id;
		var cur_table = $detail.html('<table></table>').find('table');
		$(cur_table).bootstrapTable({
			url: searchStepUrl,
			method: 'get',
			pagination: false,
			responseHandler: function(res) {
				var result = res["result"];
				return {
					"data": result
				};
			},
			queryParams: { jobId: jobid },
			ajaxOptions: { jobId: jobid },
			uniqueId: "id",
			columns: [
				{
					field: 'id',
					title: '步骤id'
				}, {
					field: 'stepCode',
					title: '步骤code'
				}, {
					field: 'stepName',
					title: '步骤名称'
				}, {
					field: 'stepStatusName',
					title: '步骤状态',
					align: 'center',
					formatter: function(value, row, index) {
						if(row.stepStatus == "S"){
							return '<h4><span class="label label-success">' + value + '</span></h4>';
						}
						else if(row.stepStatus == "K"){
							return '<h4><span class="label label-success">' + value + '</span></h4>';
						}
						else if(row.stepStatus == "F"){
							return '<h4><span class="label label-danger">' + value + '</span></h4>';
						}
						else if(row.stepStatus == "C"){
							return '<h4><span class="label label-info">' + value + '</span></h4>';
						}
						else if(row.stepStatus == "R"){
							return '<h4><span class="label label-warning">' + value + '</span></h4>';
						}
					}
				}, {
					field: 'stepOrder',
					title: '步骤顺序'
				}, {
					field: 'execCount',
					title: '执行次数'
				}, {
					field: 'execTimeFormat',
					title: '执行时间'
				}, {
					field: 'stepParams',
					title: '步骤参数'
				}, {
					title: '操作',
					field: 'stepStatus',
					align: 'center',
					formatter: function(value, row, index) {
						var d = "";
						if (value == 'F') {
							//d += '<a onclick="skipStep(' + row.id + ')">跳过</a>';
							d += '<button type="button" class="btn btn-warning" onclick="skipStep(' + row.id + ')">跳过</button>';
						}
						return d;
					}
				}]
		});
	}
}

function queryParams() {
	var param = {
		params: {
			jobId: $("#jobId").val() || null,
			projectCode: $("#projectCode").val() || null,
			moduleCode: $("#moduleCode").val() || null,
			jobCode: $("#jobCode").val() || null,
			businessNo: $("#businessNo").val() || null,
			jobStatus: $("#jobStatus").val() || null,
		},
		currentPage: this.pageNumber || 1,
		pageSize: this.pageSize || 20
	};
	return param;
}

$("#searchButton").click(function() {
	$.ajax({
		type: "POST",
		url: searchUrl,
		contentType: "application/json",
		data: JSON.stringify(queryParams()),
		dataType: "json",
		success: function(rst) {
			if (rst.code != '0') {
				checkLogin(data);
				alert(data.message || data.msg);
			} else {
				var result = rst["result"];
				var data = {
					"total": result.totalItem,
					"rows": result.resultList
				};
				$("#table").bootstrapTable('load', data);
			}
		}
	});
});

function resetJob(jobIds) {
	if (confirm('确定要重启任务吗?') == true) {
		$.ajax({
			type: "GET",
			url: batchResetJobUrl,
			data: { "jobIds": jobIds },
			dataType: "json",
			success: function(data) {
				if (data.code != '0') {
					checkLogin(data);
					alert(data.message || data.msg);
				} else {
					alert('重启任务成功');
					$("#table").bootstrapTable('refresh');
				}
			}
		});
	}
}

function closeJob(jobIds) {
	if (confirm('确定要关闭任务吗?') == true) {
		$.ajax({
			type: "GET",
			url: batchCloseJobUrl,
			data: { "jobIds": jobIds },
			dataType: "json",
			success: function(data) {
				if (data.code != '0') {
					checkLogin(data);
					alert(data.message || data.msg);
				} else {
					alert('关闭任务成功');
					$("#table").bootstrapTable('refresh');
				}
			}
		});
	}
}

function skipStep(stepId) {
	if (confirm('确定要跳过该子任务吗?') == true) {
		$.ajax({
			type: "GET",
			url: skipJobStepUrl,
			data: { "stepId": stepId },
			dataType: "json",
			success: function(data) {
				if (data.code != '0') {
					checkLogin(data);
					alert(data.message || data.msg);
				} else {
					alert('跳过子任务成功');
					$("#table").bootstrapTable('refresh');
				}
			}
		});
	}
}


// 批量关闭
$("#batchClose").click(function() {
	var rows = $("#table").bootstrapTable('getSelections');
	if (rows.length == 0) {
		alert("请先选择要删除的记录!");
		return;
	}
	var jobIds = '';
	for (var i = 0; i < rows.length; i++) {
		jobIds += rows[i]['id'] + ",";
	}
	jobIds = jobIds.substring(0, jobIds.length - 1);
	closeJob(jobIds);
});

$("#batchRestart").click(function() {
	var rows = $("#table").bootstrapTable('getSelections');
	if (rows.length == 0) {
		alert("请先选择要重启的记录!");
		return;
	}
	var jobIds = '';
	for (var i = 0; i < rows.length; i++) {
		jobIds += rows[i]['id'] + ",";
	}
	jobIds = jobIds.substring(0, jobIds.length - 1);
	resetJob(jobIds);
});

